package com.xk.service.impl;

import com.xk.mapper.TeacherMapper;
import com.xk.pojo.Page;
import com.xk.pojo.Teacher;
import com.xk.service.TeacherService;
import com.xk.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Service
public class TeacherServiceImpl implements TeacherService {

    private TeacherMapper mapper;

    @Override
    public List<Teacher> getAllTeachers() {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        mapper = sqlSession.getMapper(TeacherMapper.class);
        List<Teacher> teachers = mapper.getAllTeachers();
        for (Teacher teacher : teachers) {
            System.out.println(teacher);
        }
        sqlSession.close();
        return teachers;
    }

    @Override
    public Teacher getTeacherById(Integer id) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        mapper = sqlSession.getMapper(TeacherMapper.class);
        Teacher teacher = mapper.getTeacherById(id);
        sqlSession.commit();
        sqlSession.close();
        return teacher;
    }

    @Override
    public int deleteTeacherById(Integer id) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        mapper = sqlSession.getMapper(TeacherMapper.class);
        int i = mapper.deleteTeacherById(id);
        sqlSession.commit();
        sqlSession.close();
        return i;
    }

    @Override
    public int updateTeacherById(Teacher teacher) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        mapper = sqlSession.getMapper(TeacherMapper.class);
        int i = mapper.updateTeacherById(teacher);
        sqlSession.commit();
        sqlSession.close();
        return i;
    }


    @Override
    public Page getTeacherListByPage(Integer pageNumber, Integer pageSize,
                                     String search) {
        System.out.println("teacherServiceImpl");
        System.out.println("pageNumber:"+pageNumber+" pageSize："+pageSize+" search:"+search);
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        mapper = sqlSession.getMapper(TeacherMapper.class);
        List<Teacher> teachers;
        if (search.equals("")||search==null){
            teachers = mapper.getAllTeachers();
        }else {
            teachers=mapper.getTeacherByName(search);

        }

        Page page=new Page(pageNumber,pageSize,teachers.size());
        System.out.println();
        List<Teacher> teacherList
                = mapper.getTeacherListByPage((pageNumber - 1) * pageSize,
                pageSize,search);

        page.setDataList(teacherList);

        sqlSession.close();
        return page;
    }

    @Override
    public String  addTeacher(Teacher teacher) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        mapper = sqlSession.getMapper(TeacherMapper.class);
        int i = mapper.addTeacher(teacher);
        sqlSession.commit();
        sqlSession.close();
        if (i==1)
            return "添加成功";
        else
            return "添加失败";

    }
}
